package com.totoro.tygeneratedragonblock.executor.computeparse.impl;

import com.totoro.tygeneratedragonblock.constants.ComputeTypeConst;
import com.totoro.tygeneratedragonblock.data.ComputeResult;
import com.totoro.tygeneratedragonblock.executor.computeparse.ComputeParseExecutor;
import com.totoro.tygeneratedragonblock.utils.PapiUtil;
import com.totoro.tygeneratedragonblock.utils.StrUtil;
import org.bukkit.entity.Player;

import java.util.concurrent.ThreadLocalRandom;

/**
 * @author Totoro
 * @version 0.1.0
 * @date 2025/8/10 - 22:06
 * @description 对于{random:标签的处理}
 **/
public class RandomComputeParseExecutor implements ComputeParseExecutor {
    @Override
    public ComputeResult execute(Player player, String computeText) {
        String parsePapiContent = PapiUtil.parseStrPapiVariable(player, computeText);
        // 获取random:后的内容，这个内容是用来随机值的，比如1-10
        String finalContent = StrUtil.getPrefixAfter(parsePapiContent, ComputeTypeConst.RANDOM);
        // 通过-进行分割，获取最小和最大值
        String[] ranges = finalContent.split("-");
        String minStr = ranges[0];
        String maxStr = ranges[1];
        // 先将字符串转成Double
        int min = (int) Math.round(Double.parseDouble(minStr));
        int max = (int) Math.round(Double.parseDouble(maxStr));
        // 计算随机值
        int finalResult = ThreadLocalRandom.current().nextInt(min, max + 1);
        return new ComputeResult(ComputeTypeConst.RANDOM, finalResult, null);
    }
}
